#! /bin/bash

# ldpc_check
#
# A series of checks of the ldpc functions, mostly decode.
# 
# This uses ldpc_enc to supply test data to ldpc_dec and mosty
# assumes that the encode function is correct.

# Define macros to (later) allow testing alternate versions.
if [[ $1 == "--test" ]]; then
    alias LDPC_ENC=ldpc_enc_test
    alias LDPC_DEC=ldpc_dec_test
else
    alias LDPC_ENC=ldpc_enc
    alias LDPC_DEC=ldpc_dec
fi
shopt -s expand_aliases

# PATH
PATH=$PATH:../src

PASS=1

###############################
echo
echo "Simple test, HRA_112_112, ideal"
LDPC_ENC /dev/zero - --sd --code HRA_112_112 --testframes 100 |
    LDPC_DEC - /dev/null --code HRA_112_112 --sd --testframes 2> tmp
cat tmp
n=$(grep 'BER: 0.000' tmp | wc -l)
if [ $n == 2 ]; then echo "OK"; else echo "BAD"; PASS=0; fi

###############################
echo
echo "Simple test, HRA_112_112, 1dB"
LDPC_ENC /dev/zero - --sd --code HRA_112_112 --testframes 100 |
    ldpc_noise - - 1 |
    LDPC_DEC - /dev/null --code HRA_112_112 --sd --testframes 2>tmp
cat tmp
n=$(grep 'Raw.*BER:' tmp | cut -d ' ' -f 7)
p1=$(echo $n '<=' 0.11 | bc)
n=$(grep 'Coded.*BER:' tmp | cut -d ' ' -f 7)
p2=$(echo $n '<=' 0.015 | bc)
if [[ $p1 -eq 1 && $p2 -eq 1 ]]; then echo "OK"; else echo "BAD"; PASS=0; fi


###############################
echo
echo "Simple test, default, ideal"
LDPC_ENC /dev/zero - --sd --testframes 10 |
    LDPC_DEC - /dev/null --sd --testframes 2> tmp
cat tmp
n=$(grep 'BER: 0.000' tmp | wc -l)
if [ $n == 2 ]; then echo "OK"; else echo "BAD"; PASS=0; fi

###############################
echo
echo "Simple test, default, 1db"
LDPC_ENC /dev/zero - --sd --testframes 10 |
    ldpc_noise - - 1 |
    LDPC_DEC - /dev/null --sd --testframes 2> tmp
cat tmp
n=$(grep 'Raw.*BER:' tmp | cut -d ' ' -f 7)
p1=$(echo $n '<=' 0.11 | bc)
n=$(grep 'Coded.*BER:' tmp | cut -d ' ' -f 7)
p2=$(echo $n '<=' 0.11 | bc)
if [[ $p1 -eq 1 && $p2 -eq 1 ]]; then echo "OK"; else echo "BAD"; PASS=0; fi

##############################################################
# Compare original to test versions
if [[ $1 == "--test" ]]; then
    echo
    echo "Compare Versions:"

    ###############################
    echo
    echo "enc, HRA_112_112, plain"
    ldpc_enc      /dev/zero - --code HRA_112_112 --testframes 10 > tmp1
    ldpc_enc_test /dev/zero - --code HRA_112_112 --testframes 10 > tmp2
    if cmp tmp1 tmp2; then echo "OK"; else echo "BAD"; PASS=0; fi

    ###############################
    echo
    echo "enc, HRA_112_112, sd"
    ldpc_enc      /dev/zero - --sd --code HRA_112_112 --testframes 10 > tmp1
    ldpc_enc_test /dev/zero - --sd --code HRA_112_112 --testframes 10 > tmp2
    if cmp tmp1 tmp2; then echo "OK"; else echo "BAD"; PASS=0; fi

    ###############################
    echo
    echo "enc, default, plain"
    ldpc_enc      /dev/zero - --testframes 10 > tmp1
    ldpc_enc_test /dev/zero - --testframes 10 > tmp2
    if cmp tmp1 tmp2; then echo "OK"; else echo "BAD"; PASS=0; fi

    ###############################
    echo
    echo "enc, default, sd"
    ldpc_enc      /dev/zero - --sd --testframes 10 > tmp1
    ldpc_enc_test /dev/zero - --sd --testframes 10 > tmp2
    if cmp tmp1 tmp2; then echo "OK"; else echo "BAD"; PASS=0; fi

fi

echo
if [[ $PASS == 1 ]]; then echo "PASSED"; else echo "FAILED"; fi
